//
//  DeviceViewModel.m
//  ForYourBeauty
//
//  Created by Kelly xu on 2019/6/3.
//  Copyright © 2019 changrenTech. All rights reserved.
//

#import "DeviceViewModel.h"

@implementation DeviceViewModel
-(void)fetchDeviceList:(NSString*)userId cmp:(void(^)(NSMutableArray *deviceList))cmp{
    [[HttpClient shareClient]get:MYDEVICE_LIST params:@{@"userId":userId} suc:^(id results) {
        NSMutableArray *deviceList = [[NSMutableArray alloc]init];
        for (int i=0; i<[results count]; i++) {
            NSDictionary *dic = [results objectAtIndex:i];
            DeviceListModel *model = [[DeviceListModel alloc]initWithDictionary:dic];
            [deviceList addObject:model];
        }
        cmp(deviceList);
    } fail:^(NSString *error, NSInteger status) {
        Toast(error);
    }];
}
-(void)bindDevice:(NSString*)deviceId cmp:(void(^)())cmp fail:(void(^)())fail{
    [[HttpClient shareClient] postWithUrl:DEVICE_BIND params:@{@"deviceId":deviceId,@"id":[LoginTools sharedTools].userId} suc:^(id results) {
        cmp();
    } fail:^(NSString *error, NSInteger status) {
        [SVProgressHUD dismiss];
        Toast(error);
        if (status != 117) {
           fail();
        }
    }];
}
-(void)unbindDevice:(NSString*)deviceId cmp:(void(^)())cmp{
    [[HttpClient shareClient] postWithUrl:DEVICE_UNBIND params:@{@"deviceId":deviceId,@"id":[LoginTools sharedTools].userId} suc:^(id results) {
        cmp();
    } fail:^(NSString *error, NSInteger status) {
        Toast(error);
    }];
}
-(void)fetchDeviceInfo:(NSString*)deviceId cmp:(void(^)(NSDictionary *deviceInfo))cmp{
    [[HttpClient shareClient]get:DEVICE_DETAIL params:@{@"deviceId":deviceId} suc:^(id results) {
        cmp(results);
    } fail:^(NSString *error, NSInteger status) {
        [SVProgressHUD dismiss];
        Toast(error);
        [MBProgressHUD hideHUDForView:KeyWindow animated:YES];
    }];
}
-(void)optionDevice:(NSString*)deviceId option:(NSString*)option runtime:(NSInteger)runtime suc:(void(^)())suc fail:(void(^)())fail{
    [[HttpClient shareClient] postWithUrl:OPTION_DEVICE params:@{@"deviceId":deviceId,@"userId":[LoginTools sharedTools].userId,@"optionType":@"1",@"option":option,@"runTime":@(runtime)} suc:^(id results) {
        suc();
    } fail:^(NSString *error, NSInteger status) {
        Toast(error);
    }];
}
-(void)getLastDeviceUseTime:(void(^)(NSInteger time))cmp{
    [[HttpClient shareClient]get:DEVICE_LAST_USETIME params:@{@"userId":[LoginTools sharedTools].userId} suc:^(id results) {
        NSString *resultStr = [NSString stringWithFormat:@"%@",results];
        if (EC_ISNullStr(resultStr) || [resultStr isEqualToString:@"<null>"]) {
            cmp(1800);
        }else{
          cmp([results[@"time"] integerValue]);
        }
    } fail:^(NSString *error, NSInteger status) {
        Toast(error);
    }];
}
-(void)getDeviceRemainTime:(NSString*)deviceId cmp:(void(^)(DeviceContinueModel *model))cmp{

    NSTimeInterval timeInterval = [[NSDate date] timeIntervalSince1970]*1000;//现在时间,你可以输出来看下是什么格式

    NSString *timeSp = [NSString stringWithFormat:@"%ld",(long)timeInterval];
    [[HttpClient shareClient] postWithUrl:DEVICE_REMAIN_TIME params:@{@"deviceId":deviceId,@"userId":[LoginTools sharedTools].userId,@"beganTime":timeSp} suc:^(id results) {
        cmp([DeviceContinueModel modelObjectWithDictionary:results]);
    } fail:^(NSString *error, NSInteger status) {
        Toast(error);
    }];
}
@end
